package com.nbsaas.boot.shopping.ext.resource;

import com.nbsaas.boot.customer.api.apis.CustomerAddressApi;
import com.nbsaas.boot.product.api.apis.ProductApi;
import com.nbsaas.boot.product.api.apis.ProductSkuApi;
import com.nbsaas.boot.product.api.domain.response.ProductResponse;
import com.nbsaas.boot.product.api.domain.response.ProductSkuResponse;
import com.nbsaas.boot.rest.filter.Filter;
import com.nbsaas.boot.rest.response.MapResponse;
import com.nbsaas.boot.rest.response.ResponseObject;
import com.nbsaas.boot.shopping.api.apis.ShoppingCartApi;
import com.nbsaas.boot.shopping.api.domain.field.ShoppingCartField;
import com.nbsaas.boot.shopping.api.domain.simple.ShoppingCartSimple;
import com.nbsaas.boot.shopping.ext.apis.ShoppingCartCheckoutApi;
import com.nbsaas.boot.shopping.ext.domain.request.ShoppingCartCheckoutRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


@Service
public class ShoppingCartCheckoutResource implements ShoppingCartCheckoutApi {

    @Resource
    private ShoppingCartApi shoppingCartApi;

    @Resource
    private CustomerAddressApi customerAddressApi;

    @Resource
    private ProductApi productApi;

    @Resource
    private ProductSkuApi productSkuApi;


    @Transactional
    @Override
    public ResponseObject<?> checkout(ShoppingCartCheckoutRequest request) {
        ResponseObject<MapResponse> result = new ResponseObject<>();
        result.setData(new MapResponse());
        List<ShoppingCartSimple> carts = shoppingCartApi.listData(
                Filter.eq(ShoppingCartField.creator, request.getCreator()),
                Filter.eq(ShoppingCartField.checked, true)
        );
        if (carts == null) {
            return result;
        }

        List<ShoppingCartSimple> skuList=new ArrayList<>();
        //检查库存
        //同步价格
        for (ShoppingCartSimple cart : carts) {
            ProductResponse product = productApi.oneData(Filter.eq("id", cart.getProduct()));
            if (product.getSkuEnable()) {
                ProductSkuResponse sku = productSkuApi.oneData(Filter.eq("id", cart.getSku()));
                if (sku == null) {
                    continue;
                }
                cart.setPrice(sku.getPrice());
            } else {
                cart.setPrice(product.getPrice());
            }
            skuList.add(cart);
        }

        result.getData().put("items", skuList);

        int sum = carts.stream().mapToInt(ShoppingCartSimple::getNum).sum();
        result.getData().put("sum", sum);
        BigDecimal total = carts.stream()
                .map(item -> item.getPrice().multiply(new BigDecimal(item.getNum())))
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        result.getData().put("total", total);
        result.getData().put("freightPrice", BigDecimal.ZERO);

        if (request.getAddress() != null) {
            result.getData().put("address", customerAddressApi.oneData(Filter.eq("id", request.getAddress())));
        }

        //检查库存
        return result;
    }
}
